package com.scmt.hmzd.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.scmt.hmzd.entity.HmDisableDate;
import com.scmt.hmzd.service.IHmDisableDateService;
import com.scmt.core.common.vo.PageVo;
import com.scmt.core.common.vo.SearchVo;
import com.scmt.hmzd.mapper.HmDisableDateMapper;
import com.scmt.core.utis.FileUtil;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.LinkedHashMap;
import java.util.List;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Map;

/**
 * @author
 **/
@Service
public class HmDisableDateServiceImpl extends ServiceImpl<HmDisableDateMapper, HmDisableDate> implements IHmDisableDateService {
    @Autowired
    @SuppressWarnings("SpringJavaAutowiringInspection")
    private HmDisableDateMapper hmDisableDateMapper;

    @Override
    public IPage<HmDisableDate> queryHmDisableDateListByPage(HmDisableDate hmDisableDate, SearchVo searchVo, PageVo pageVo) {
        int page = 1;
        int limit = 10;
        if (pageVo != null) {
            if (pageVo.getPageNumber() != 0) {
                page = pageVo.getPageNumber();
            }
            if (pageVo.getPageSize() != 0) {
                limit = pageVo.getPageSize();
            }
        }
        Page<HmDisableDate> pageData = new Page<>(page, limit);
        QueryWrapper<HmDisableDate> queryWrapper = new QueryWrapper<>();
        if (hmDisableDate != null) {
            queryWrapper = LikeAllField(hmDisableDate, searchVo);
        }
        IPage<HmDisableDate> result = hmDisableDateMapper.selectPage(pageData, queryWrapper);
        return result;
    }

    @Override
    public void download(HmDisableDate hmDisableDate, HttpServletResponse response) {
        List<Map<String, Object>> mapList = new ArrayList<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        QueryWrapper<HmDisableDate> queryWrapper = new QueryWrapper<>();
        if (hmDisableDate != null) {
            queryWrapper = LikeAllField(hmDisableDate, null);
        }
        List<HmDisableDate> list = hmDisableDateMapper.selectList(queryWrapper);
        for (HmDisableDate re : list) {
            Map<String, Object> map = new LinkedHashMap<>();
            mapList.add(map);
        }
        FileUtil.createExcel(mapList, "exel.xlsx", response);
    }

    /**
     * 功能描述：构建模糊查询
     *
     * @param hmDisableDate 需要模糊查询的信息
     * @return 返回查询
     */
    public QueryWrapper<HmDisableDate> LikeAllField(HmDisableDate hmDisableDate, SearchVo searchVo) {
        QueryWrapper<HmDisableDate> queryWrapper = new QueryWrapper<>();
        if (hmDisableDate.getId() != null) {
            queryWrapper.and(i -> i.like("hm_disable_date.id", hmDisableDate.getId()));
        }
        if (StringUtils.isNotBlank(hmDisableDate.getTechnicianId())) {
            queryWrapper.and(i -> i.eq("hm_disable_date.technician_id", hmDisableDate.getTechnicianId()));
        }
        if (hmDisableDate.getDisableDate() != null) {
            queryWrapper.and(i -> i.eq("hm_disable_date.disable_date", hmDisableDate.getDisableDate()));
        }
        if (StringUtils.isNotBlank(hmDisableDate.getDisableTime())) {
            queryWrapper.and(i -> i.like("hm_disable_date.disable_time", hmDisableDate.getDisableTime()));
        }
        return queryWrapper;
    }
}
